﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Text;

namespace smartphone.UserControls
{
    public partial class AdminProductList : System.Web.UI.UserControl
    {
        private Product product = new Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            GridViewProductList.DataSource = product.GetProductForAdmin();
            GridViewProductList.DataBind();
        }

        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }

        protected void GridViewProductList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Int32.Parse(GridViewProductList.DataKeys[e.RowIndex].Value.ToString());
            try
            {
                bool success = product.DeleteProduct(id);
            }
            catch
            {
                ShowPopUpMsg("Có lỗi khi xóa sản phẩm.");
            }
            ShowPopUpMsg("Xóa thành công sản phẩm.");
            GridViewProductList.EditIndex = -1;
            BindGrid();
        }

    }
}